<?php

namespace app\api\controller\inventory;

use app\api\controller\Base;
use app\api\controller\Pub;
use app\api\controller\mytool\Sendmessage;
use app\common\model\cost\Costtype;
use app\common\model\orderdata\Storageareachange;
use app\common\model\orderdata\Goods;
use app\common\model\orderdata\StorageAreaGoods;
use app\common\model\orderdata\StorageArea;
use app\common\model\orderreceipt\ReceiptList;
use app\common\model\power\Rolepower;
use app\common\model\user\User as UserModel;

class Adjustinventory extends Base
{
	
	
	/**
     * 产生jwt加密串，参考session
     */
    public function initialize()
    {
        parent::initialize();
        self::is_login();
    }

    /**
     * 申请端微调列表
     * 通过工作台进入微调的区域列表
     * @return [type] [description]
     */
    public function warelist(){
    	Rolepower::powerguard(202005);

    	$data = input('get.');
    	
		// $data['Type'] = 'cost';
		$data = init_where($data,['state'=>'Status']);

		$list = Storageareachange::usergetlist($data,'ware');
		foreach ($list['list'] as $key => $value) {
			$list['list'][$key]['UserName'] = $value['user']['Name'];
			$list['list'][$key]['GoodName'] = $value['area']['GoodName'];
			$list['list'][$key]['Weight'] = $list['list'][$key]['Weight'].'kg';
			$list['list'][$key]['StorageAreaId'] = $value['area']['StorageAreaId'];
		}
		// dump($list);
		$list['tr']['title']  = ['危废名','时间','状态','关联区域','发起人','重量'];
		$list['tr']['key'] = ['GoodName','UpTime','Status','StorageAreaId','UserName','Weight'];
		$list['tr']['show']=[true,true,true,true,false,true];
		// $list['tr']['type'] = CostType::getlist('costview');
		// $list['tr']['typekey'] = ['id'=>'TypeId','text'=>'TypeName'];

		return myok($list);
    }

    /**
     * 管理端微调列表
     * 通过工作台进入微调的区域列表
     * @return [type] [description]
     */
    public function manalist(){
    	Rolepower::powerguard(203009,204009);

    	$data = input('get.');
		// $data['Type'] = 'cost';
		$data = init_where($data,['state'=>'Status']);
		$list = Storageareachange::usergetlist($data,'mana');
		foreach ($list['list'] as $key => $value) {
			$list['list'][$key]['UserName'] = $value['user']['Name'];
			$list['list'][$key]['GoodName'] = $value['area']['GoodName'];
			$list['list'][$key]['Weight'] = $list['list'][$key]['Weight'].'kg';
			$list['list'][$key]['StorageAreaId'] = $value['area']['StorageAreaId'];
		}

		$list['tr']['title']  = ['危废名','时间','状态','关联区域','发起人','重量'];
		$list['tr']['key'] = ['GoodName','UpTime','Status','StorageAreaId','UserName','Weight'];
		$list['tr']['show']=[true,true,true,true,true,false];
		// $list['tr']['type'] = CostType::getlist('costview');
		// $list['tr']['typekey'] = ['id'=>'TypeId','text'=>'TypeName'];

		return myok($list);
    }

    /**
     * 库管区域微调列表页面
     * @return [type] [description]
     */
	public function warearealist()
	{
       	Rolepower::powerguard(202005);
		$data = input('get.');
		$data = init_where($data);

		$data['order'] = 'GoodId desc';
		$area_good = StorageAreaGoods::where('StorageAreaId', $data['area_id'])->select()->toArray();
		$good_id = array_column($area_good, 'GoodId');
		$good = Goods::where('GoodId', 'in', $good_id)->select()->toArray();
		$list = ['list' => $good];

		foreach ($list['list'] as &$v) {
				$v['storageAreaGoods'] = StorageAreaGoods::where('GoodId', $v['GoodId'])->where('StorageAreaId', $data['area_id'])->select()->toArray();

			if (!empty($v['storageAreaGoods'])) {
				foreach ($v['storageAreaGoods'] as &$val) {
					$change = Storageareachange::where([
						'StorageAreaGoodsId' => $val['StorageAreaGoodsId'],
						'GoodId' => $val['GoodId']
					])->order('StorageAreaChangeId desc')->find();
					if (!empty($change)) {
						$val['Status'] = $change->Status;
					} else {
						// 暂未微调
						$val['Status'] = 0;
					}
				}
			}
		}
		unset($v);
//		$list['tr']['type'] = CostType::getlist('costview');
//		$list['tr']['typekey'] = ['id'=>'TypeId','text'=>'TypeName'];
		$list['area_info'] =  StorageArea::get($data['area_id']);

		$list['area_info'] = $list['area_info']->hidden(['OperationLog']);
		return myok($list);
	}

	public function detailsLists()
	{
		$param = input('get.');
		$model = new Storageareachange();
		$option = come_model_option($param, $model);
		$rdata = $option['rdata'];
		$data = $option['data'];
		$rdata = $rdata->select();
//		$rdata->visible($visible);
		$ndata['list'] = $rdata;
		$ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
		$list = $ndata;
		foreach ($list['list'] as &$v) {
			$user_name = $v->user->Name ?? '';
			$good_name = $v->good->Name ?? '';
			$v->UpUserName = $user_name;
			$v->GoodName = $good_name;
		}
		unset($v);
		return myok($list);
	}
	/**
	 * 会计股东查看的区域列表
	 * @return [type] [description]
	 */
	public function manaarealist()
	{
		Rolepower::powerguard(203009,204009);
		$id = input('area_id');
		$res = StorageArea::getchangelist($id);
		return myok($res);
	}

	/**
	 * 添加待审核的微调订单
	 * @return \think\response\Json
	 */
	public function adjust()
	{
 		Rolepower::powerguard(202005);
		$param = $this->request->post();
		if(empty($param['StorageAreaGoodsId'])){
			return myerr('缺少参数');
		}
		$sb = StorageAreaGoods::where('StorageAreaGoodsId',$param['StorageAreaGoodsId'])->find();
		if(empty($sb)){
			return myerr('没找到微调的产品信息');
		}
		$as = StorageArea::where($sb['StorageAreaId']);
		if(empty($as)){
			return myerr('没找到库存区域信息');
		}
		try {
			$sc = Storageareachange::create([
				'StorageAreaGoodsId' => $param['StorageAreaGoodsId'],
				'UpUserId'           => Pub::userid(),
				'UpTime'             => date('Y-m-d H:i:s'),
				'Weight'             => $param['Weight'],
				'BeforeWeight'       => $param['BeforeWeight'],
				'Status'             => 1,
				'GoodId'             => $param['GoodId']
			]);
			Sendmessage::reviewadjust(1,$sc->StorageAreaChangeId);
			return myok();
		} catch (Exception $e) {
			return myerr();
		}

	}

    /**
     * 判断账号是否登录
     * @return boolean [description]
     */
    private function is_login()
    {
        $data = UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
}
